<template>
	<view class="content">
		<!-- <uni-popup ref="popup" @change="change">
			<view class="popup-content">
				<view class="warp">
					<button class="button" type="primary" @click="setFormType(1)">吊装令</button>
					<button class="button" type="primary" @click="setFormType(2)">动火令</button>
					<button class="button" type="primary" @click="setFormType(3)">动土令</button>
				</view>
			</view>
		</uni-popup> -->
		<view class="content-form" v-show="state==1">
			<uni-forms ref="baseFormJCD" :rules="rules" :modelValue="formData" label-position="top">
				<uni-forms-item label="车辆吨位(吨)" required name="vehicleTonnage">
					<uni-easyinput type="digit" v-model="formData.vehicleTonnage" placeholder="请输入车辆吨位" />
				</uni-forms-item>
				<uni-forms-item label="车牌号" required name="licensePlate">
					<uni-easyinput v-model="formData.licensePlate" placeholder="请输入车牌号" />
				</uni-forms-item>
				<uni-forms-item label="使用单位" required name="useUnit">
					<uni-easyinput v-model="formData.useUnit" placeholder="请输入使用单位" />
				</uni-forms-item>
				<uni-forms-item label="使用地点" required name="useAddress">
					<uni-easyinput v-model="formData.useAddress" placeholder="请输入使用地点" />
				</uni-forms-item>
				<uni-forms-item label="作业内容" required name="workContent">
					<uni-easyinput v-model="formData.workContent" placeholder="请输入作业内容" />
				</uni-forms-item>

				<uni-forms-item label="指挥人员" required name="commandId">
					<uni-data-picker placeholder="请选择吊装指挥人员" popup-title="请选择吊装指挥人员" :localdata="userList"
						v-model="formData.commandId" @change="e=>getSelectUserName(e,'commandName')">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="操作人员" required name="operativesId">
					<!-- <uni-easyinput v-model="formData.operativesId" placeholder="请输入操作人员" /> -->
					<uni-data-picker placeholder="请选择操作人员" popup-title="请选择操作人员" :localdata="userList"
						v-model="formData.operativesId" @change="e=>getSelectUserName(e,'operatives')">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="现场技术员" required name="technicianId">
					<uni-data-picker placeholder="请选择现场技术员" popup-title="请选择现场技术员" :localdata="userList"
						v-model="formData.technicianId" @change="e=>getSelectUserName(e,'technicianName')">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="现场安全员" required name="safetyOfficerId">
					<uni-data-picker placeholder="请选择现场安全员" popup-title="请选择现场安全员" :localdata="userList"
						v-model="formData.safetyOfficerId" @change="e=>getSelectUserName(e,'safetyOfficerName')">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="网格员" required name="gridLengthId">
					<uni-data-picker placeholder="请选择网格员" popup-title="请选择网格员" :localdata="userList"
						v-model="formData.gridLengthId" @change="e=>getSelectUserName(e,'gridLengthName')">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="现场专监" required name="siteSupervisionId">
					<uni-data-picker placeholder="请选择现场专监" popup-title="请选择现场专监" :localdata="userList"
						v-model="formData.siteSupervisionId" @change="e=>getSelectUserName(e,'siteSupervisionName')">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="安全专监" required name="safetySupervisionId">
					<uni-data-picker placeholder="请选择安全专监" popup-title="请选择安全专监" :localdata="userList"
						v-model="formData.safetySupervisionId"
						@change="e=>getSelectUserName(e,'safetySupervisionName')">
					</uni-data-picker>
				</uni-forms-item>
				<uni-section class="mb-10" title="检查要点" type="line"></uni-section>
				<view class="jcydBox" v-for="(item,index) in formData.inspectionPoints">
					<view class="jcydItem">
						<view class="text">
							{{item.text}}
						</view>
						<uni-data-checkbox v-model="item.value" :localdata="isCheckBoxList"></uni-data-checkbox>
					</view>
					<view class="xian">

					</view>
				</view>

			</uni-forms>
			<button type="primary" @click="handleNext('baseFormJCD')">下一步</button>
		</view>
		<view class="content-form" v-show="state==2">
			<uni-forms ref="baseForm" :rules="rules" :modelValue="formData" label-position="top">
				<uni-forms-item label="施工部位" required name="constructionSite">
					<uni-easyinput v-model="formData.constructionSite" placeholder="请输入施工部位" />
				</uni-forms-item>
				<uni-forms-item label="作业班组" required name="workTeam">
					<!-- <uni-data-picker placeholder="请选择作业班组" popup-title="请选择作业班组" :localdata="dataTree"
						v-model="formData.workTeam">
					</uni-data-picker> -->
					<uni-easyinput v-model="formData.workTeam" placeholder="请输入作业班组" />
				</uni-forms-item>
				<uni-forms-item label="申请事由" required name="reasonForApplication">
					<uni-easyinput type="textarea" v-model="formData.reasonForApplication" placeholder="请输入申请事由" />
				</uni-forms-item>
				<uni-forms-item label="开始时间" required name="startTime">
					<uni-datetime-picker type="date" :clear-icon="false" v-model="formData.startTime"
						placeholder="请选择开始时间" />
				</uni-forms-item>
				<uni-forms-item label="结束时间" required name="endTime">
					<uni-datetime-picker type="date" :clear-icon="false" v-model="formData.endTime"
						placeholder="请选择结束时间" />
				</uni-forms-item>

				<uni-forms-item label="起重设备" required name="liftingEquipment">
					<uni-data-picker placeholder="请选择起重设备" popup-title="请选择起重设备" :localdata="equipmentList"
						v-model="formData.liftingEquipment">
					</uni-data-picker>
					<!-- <uni-easyinput v-model="formData.liftingEquipment" placeholder="请输入起重设备" /> -->
				</uni-forms-item>
				<uni-forms-item label="设备编号" required name="equipmentCode">
					<uni-easyinput v-model="formData.equipmentCode" placeholder="请输入设备编号" />
				</uni-forms-item>
				<uni-forms-item label="吊装级别" required name="level">
					<uni-data-picker placeholder="请选择吊装级别" popup-title="请选择吊装级别" :localdata="levelList"
						v-model="formData.level">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="吊装人员" required name="operativesId">
					<!-- <uni-easyinput v-model="formData.operatives" placeholder="请输入吊装人员" /> -->
					<uni-data-picker placeholder="请选择吊装人员" popup-title="请选择操作人员" :localdata="userList"
						v-model="formData.operativesId" @change="e=>getSelectUserName(e,'operatives')">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="作业证件" required name="homeworkCertificate">
					<uni-file-picker v-model="imageValue" limit="3" title="最多选择3张图片" @select="select"
						@delete="delIMG"></uni-file-picker>
				</uni-forms-item>
				<uni-forms-item label="吊装指挥" required name="commandId">
					<uni-data-picker placeholder="请选择吊装指挥人员" popup-title="请选择吊装指挥人员" :localdata="userList"
						v-model="formData.commandId" @change="e=>getSelectUserName(e,'commandName')">
					</uni-data-picker>
				</uni-forms-item>

				<uni-forms-item label="现场监护人" required name="guardianId">
					<uni-data-picker placeholder="请选择监护人员" popup-title="请选择监护人员" :localdata="userList"
						v-model="formData.guardianId" @change="e=>getSelectUserName(e,'guardianName')">
					</uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="班组负责人" required name="applicationManagerId">
					<uni-data-picker placeholder="请选择班组负责人" popup-title="请选择班组负责人" :localdata="userList"
						v-model="formData.applicationManagerId"
						@change="e=>getSelectUserName(e,'applicationManagerName')">
					</uni-data-picker>
				</uni-forms-item>
				<uni-section class="mb-10" title="安全措施" type="line"></uni-section>
				<view class="jcydBox" v-for="(item,index) in formData.remark">
					<view class="jcydItem">
						<view class="text">
							{{item.text}}
						</view>
						<uni-data-checkbox v-model="item.value"
							:localdata="[{text:'是',value:'1'},{text:'否',value:'2'}]"></uni-data-checkbox>
					</view>
					<view class="xian">

					</view>
				</view>
			</uni-forms>

			<button type="primary" @click="submit('baseForm')">提交</button>
			<button style="margin-top: 20px;" @click="handlePrev()">上一步</button>
		</view>

	</view>
</template>

<script>
	import {
		listUser
	} from "@/api/system/user.js"
	import {
		addHomework
	} from "@/api/homework";
	import config from '@/config'
	import {
		getToken
	} from '@/utils/auth'
	export default {
		data() {
			return {
				show: false,
				formData: {
					id: null,
					type: 1,
					applicationTime: null,
					constructionSite: null,
					workTeam: null,
					liftingEquipment: null,
					equipmentCode: null,
					reasonForApplication: null,
					hotWorkType: null,
					level: null,
					startTime: null,
					endTime: null,
					operatives: null,
					operativesId: null,
					depth: null,
					supportMethod: null,
					peopleCount: null,
					homeworkCertificate: [],
					commandId: null,
					commandName: null,
					guardianId: null,
					guardianName: null,
					applicationManagerId: null,
					applicationManagerName: null,
					technicalDepartment: null,
					securitySector: null,
					projectLeader: null,
					status: 0,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null,
					remark: [{
							text: "1、大雨和六级及以上大风严禁吊装作业。",
							value: 0
						},
						{
							text: "2、起重设备工况（吊索吊具、机械结构、安全装置、限位）是否合格有效。",
							value: 0,
						},
						{
							text: "3、吊装范围及附近的电力线、管道、建筑物等安全距离是否确认，并采取有效安全措施。",
							value: 0,
						},
						{
							text: "4、汽车吊支腿未全部打开并按规定要求铺设垫木。",
							value: 0
						},
						{
							text: "5、起重设备通行及进行吊装作业的道路和场所是否平整、坚实。",
							value: 0,
						},
						{
							text: "6、吊装作业区域是否设置警戒区域或警戒区小于物体坠落影响范围。",
							value: 0,
						},
						{
							text: "7、作业人员是否进行方案及安全技术措施交底。",
							value: 0
						},
						{
							text: "8、作业人员劳动防护用品配备符合安全作业要求。",
							value: 0
						},
						{
							text: "9、其他相关特殊作业已办理相应安全作业票。",
							value: 0
						},
					],
					vehicleTonnage: null,
					licensePlate: null,
					useUnit: null,
					useAddress: null,
					workContent: null,
					technicianId: null,
					technicianName: null,
					safetyOfficerId: null,
					safetyOfficerName: null,
					gridLengthId: null,
					gridLengthName: null,
					siteSupervisionId: null,
					siteSupervisionName: null,
					safetySupervisionId: null,
					safetySupervisionName: null,
					inspectionPoints: [{
						text: '1.吊钩有防止吊重意外脱钩的保险装置',
						value: 0
					}, {
						text: '2.吊重不大于其额定负荷',
						value: 0
					}, {
						text: '3.吊钩表面应光洁、无剥裂、锐角、毛刺、裂纹，无焊补，磨损≤10%',
						value: 0
					}, {
						text: '4.绳卡、卸扣规格型号及安装符合要求，无变形、裂纹',
						value: 0
					}, {
						text: '5.钢丝绳润滑状态良好',
						value: 0
					}, {
						text: '6.钢丝绳无扭结、死弯、塑性变形',
						value: 0
					}, {
						text: '7.钢绳无断丝或断股的现象',
						value: 0
					}, {
						text: '8.上升（运行）极限位置限制器',
						value: 0
					}, {
						text: '9.回转锁定装置',
						value: 0
					}, {
						text: '10.超载保护装置',
						value: 0
					}, {
						text: '11.吊物和起重臂下严禁站人',
						value: 0
					}, {
						text: '12.现场作业环境符合回转半径',
						value: 0
					}, {
						text: '13.俯仰角指示器、水平仪等完好',
						value: 0
					}, {
						text: '14.司机、指挥、司索工持特种作业证上岗，证件在有效期内',
						value: 0
					}, {
						text: '15.起重机在规定的架空线路安全距离范围外作业',
						value: 0
					}, {
						text: '16.现场设置安全警戒区、有警戒标志、有专人警戒',
						value: 0
					}, {
						text: '17.作业场地平坦坚实，地基承载力满足要求。与沟渠、基坑保持安全距离',
						value: 0
					}, {
						text: '18.作业前，全部伸出支腿，在撑脚下垫方木/钢板，回转支承面保持水平',
						value: 0
					}, {
						text: '19.试吊未出现支腿沉陷、起重机倾斜等情况',
						value: 0
					}],
				},
				// 表单校验
				rules: {
					constructionSite: {
						rules: [{
							required: true,
							errorMessage: "施工部位不能为空",

						}]
					},
					workTeam: {
						rules: [{
							required: true,
							errorMessage: "作业班组不能为空",

						}]
					},
					liftingEquipment: {
						rules: [{
							required: true,
							errorMessage: "起重设备不能为空",

						}]
					},
					equipmentCode: {
						rules: [{
							required: true,
							errorMessage: "设备编号不能为空",

						}]
					},
					reasonForApplication: {
						rules: [{
							required: true,
							errorMessage: "申请事由不能为空",

						}]
					},
					hotWorkType: {
						rules: [{
							required: true,
							errorMessage: "动火类型不能为空",
							trigger: "change"
						}]
					},
					level: {
						rules: [{
							required: true,
							errorMessage: "级别不能为空",

						}]
					},
					startTime: {
						rules: [{
							required: true,
							errorMessage: "开始时间不能为空",

						}]
					},
					endTime: {
						rules: [{
							required: true,
							errorMessage: "结束时间不能为空",

						}]
					},
					operativesId: {
						rules: [{
							required: true,
							errorMessage: "吊装人员不能为空",

						}]
					},
					depth: {
						rules: [{
							required: true,
							errorMessage: "深度不能为空",

						}]
					},
					supportMethod: {
						rules: [{
							required: true,
							errorMessage: "支护方式不能为空",

						}]
					},
					peopleCount: {
						rules: [{
							required: true,
							errorMessage: "作业人数不能为空",

						}]
					},
					homeworkCertificate: {
						rules: [{
							required: true,
							errorMessage: "作业证件不能为空",

						}]
					},
					commandId: {
						rules: [{
							required: true,
							errorMessage: "指挥人不能为空",

						}]
					},
					guardianId: {
						rules: [{
							required: true,
							errorMessage: "监护人不能为空",

						}]
					},
					applicationManagerId: {
						rules: [{
							required: true,
							errorMessage: "申请班组负责人不能为空",

						}]
					},
					vehicleTonnage: {
						rules: [{
							required: true,
							errorMessage: "车辆吨位不能为空",

						}]
					},
					licensePlate: {
						rules: [{
							required: true,
							errorMessage: "车牌号不能为空",

						}]
					},
					useUnit: {
						rules: [{
							required: true,
							errorMessage: "使用单位不能为空",

						}]
					},
					useAddress: {
						rules: [{
							required: true,
							errorMessage: "使用地点不能为空",

						}]
					},
					workContent: {
						rules: [{
							required: true,
							errorMessage: "作业内容不能为空",

						}]
					},
					technicianId: {
						rules: [{
							required: true,
							errorMessage: "现场技术员不能为空",

						}]
					},
					safetyOfficerId: {
						rules: [{
							required: true,
							errorMessage: "现场安全员不能为空",

						}]
					},
					gridLengthId: {
						rules: [{
							required: true,
							errorMessage: "网格员不能为空",

						}]
					},
					siteSupervisionId: {
						rules: [{
							required: true,
							errorMessage: "现场专监不能为空",

						}]
					},
					safetySupervisionId: {
						rules: [{
							required: true,
							errorMessage: "安全专监不能为空",

						}]
					},
				},
				dataTree: [{
						text: "木工班组",
						value: "木工班组",
					},
					{
						text: "架子工班组",
						value: "架子工班组",
					},

				],
				levelList: [{
					text: "一级",
					value: "1",
				}, {
					text: "二级",
					value: "2",
				}, {
					text: "三级",
					value: "3",
				}, ],
				userList: [],
				hotWorkList: [{
					text: "电焊",
					value: "1",
				}, {
					text: "气焊",
					value: "2",
				}, {
					text: "气割",
					value: "3",
				}, {
					text: "热熔",
					value: "4",
				}, {
					text: "其他",
					value: "5",
				}, ],
				imageValue: [],
				state: 1,
				equipmentList: [{
					text: '汽车吊',
					value: '汽车吊'
				}, {
					text: '履带吊',
					value: '履带吊'
				}],
				isCheckBoxList: [{
					text: '通过',
					value: 1
				}, {
					text: '不通过',
					value: 2
				}]
			}
		},
		mounted() {
			this.formData.createBy = this.$store.getters.name
			this.getData()
		},
		methods: {
			change(e) {
				this.show = e.show
			},
			setFormType(value) {
				this.formData.type = value
				this.$refs.popup.close()
			},
			getData() {
				listUser().then((res) => {
					this.userList = res.data.map((item) => {
						return {
							text: item.nickName,
							value: item.userId,
							deptId: item.deptId
						}
					})
				})
			},
			submit(ref) {
				this.$refs[ref].validate().then(res => {
					this.formData.remark = JSON.stringify(this.formData.remark)
					this.formData.inspectionPoints = JSON.stringify(this.formData.inspectionPoints)
					this.formData.homeworkCertificate = this.formData.homeworkCertificate.join(',')
					addHomework(this.formData).then((response) => {
						this.$modal.msgSuccess("新增成功");
						this.$tab.reLaunch('/pages/index')
					});
				}).catch(err => {
					console.log('err', err);
				})
			},
			handleNext(ref) {
				this.$refs[ref].validate().then(res => {
					this.state = 2
				}).catch(err => {
					console.log('err', err);
				})
			},
			handlePrev() {
				this.state = 1
			},
			getSelectUserName(value, key) {

				this.formData[key] = value.detail.value[0].text
				console.log(this.formData)
			},
			//图片上传
			select(e) {
				console.log(e);
				const tempFilePaths = e.tempFilePaths;
				let that = this

				tempFilePaths.forEach(item => {
					this.imageValue.push({
						url: item.path,
						name: ""
					})

					uni.uploadFile({
						url: config.baseUrl + "/common/upload",
						// 上传的文件
						filePath: item,
						name: 'file',

						// 请求头，设置token
						header: {
							'Authorization': getToken()
						},
						// 上传成功的回调函数
						success: (uploadFileRes) => {
							let res = JSON.parse(uploadFileRes.data)
							if (res.code == '200') {
								that.formData.homeworkCertificate.push(res.fileName)
								that.imageValue.pop()
								that.imageValue.push({
									url: config.baseUrl + res.fileName,
									name: res.fileName
								})
								console.log(that.imageValue);
							} else {
								that.imageValue.pop()
								uni.showToast({
									title: "上传失败",
									icon: "none"
								})
							}

						},
						fail: error => {

						}
					});
				})

			},
			//图片删除
			delIMG(e) {


				this.imageValue.splice(e.index, 1);
				this.formData.homeworkCertificate.splice(e.index, 1);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.popup-content {
		background-color: #fff;
		padding: 15px;
		width: 300px;

		.warp {

			.button {
				background-color: #248aff;
				margin-bottom: 10px;
			}
		}


	}

	.content-form {
		padding: 15px;
		background-color: #fff;
	}

	::v-deep .uni-forms-item {
		margin-bottom: 17px;
	}

	::v-deep .uni-forms-item__label {
		width: 180px !important;
	}

	.jcydBox {
		margin: 10px 0 0 0;

		.jcydItem {
			margin-bottom: 10px;

			.text {
				margin-bottom: 5px;
			}
		}

		.xian {
			width: 100%;
			height: 1px;
			background-color: #b9b9b9;
		}
	}

	::v-deep .checklist-box {
		padding: 5px 0;
		padding-right: 25px;
		margin: 0;
	}
</style>